import { mode as DEV_MODE } from './mode' // * DON'T DELETE
import { app, BrowserWindow, screen, Menu,net,ipcMain} from 'electron'
import * as iconvLite from'iconv-lite';
  

const test =(event:any)=>{
  let request = net.request("http://hq.sinajs.cn/list=sz000610,sz002610")
	
	  request.on('response',(response)=>{  
	      console.log(response.statusCode);
	      console.log(response.headers); 
	
	      response.on('data',(chunk)=>{ 
          var str  = chunk.toString() 
           // event.sender.send('asynchronous-reply',iconvLite.decode(chunk, 'gbk'));
           /*
           var hq_str_sz000610="��������,9.350,9.840,8.890,9.350,8.860,8.880,8.890,16901863,151434699.440,231437,8.880,289800,8.870,1237600,8.860,0,0.000,0,0.000,15100,8.890,29400,8.900,27100,8.910,37700,8.920,4200,8.930,2021-09-13,10:34:09,00";
           var hq_str_sz002610="�����Ƽ�,5.460,5.470,5.310,5.730,5.210,5.310,5.320,333739917,1813675169.470,15100,5.310,1093500,5.300,452600,5.290,602000,5.280,369800,5.270,345900,5.320,35000,5.330,72500,5.340,333100,5.350,198600,5.360,2021-09-13,10:34:09,00";
           */    
          var arr = str.split(";\n").filter(e=>e)
         
          var all = arr.map(line=>{ 
            if(line){
              var lineArr = line.split("=")
              var values  = line.split("=")[1].split(",")
              return [lineArr[0].replace("var hq_str_",""), ...values ]
            }
          })
           console.log(all); 
           event.sender.send('asynchronous-reply',chunk.toString());
	      }) 
	      response.on('end',()=>{
	        console.log('end'); 
	      }) 
	  })
	  
 	 request.end(); 
}

ipcMain.on("sendMessage",(event:any,data:any) => {
  test(event)
})
app.whenReady().then(() => {
  const screenSize = screen.getPrimaryDisplay().size
  const win = new BrowserWindow({
    width: screenSize.width ,
   // height: screenSize.height - 170,
   height:70,
   frame: false, 
    transparent: true,
    x:0,
    y:screenSize.height - 70,
    webPreferences: {
      
      devTools: false,
      nodeIntegration: true,
      contextIsolation: false
    } 

  })
  
  win.webContents.openDevTools() 
  win.setAlwaysOnTop(true)
  win.setIgnoreMouseEvents(true)//  取消鼠标 
  if (DEV_MODE === 'dev') { 
    win.loadURL('http://localhost:3003/') 
  } else {
    Menu.setApplicationMenu(null)
    win.loadFile('dist/view/index.html')
  } 
 
})
 
try { 
  require('electron-reloader')(module)
} catch {

}  
